package cn.kgc.springcloud.controller;

import cn.kgc.springcloud.client.ClazzClient;
import cn.kgc.springcloud.entity.Clazz;
import cn.kgc.springcloud.entity.Group;
import cn.kgc.springcloud.vo.GroupVO;
import cn.kgc.springcloud.service.GroupService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: mosin
 * @version: v1.0  2023/1/11
 */
@RestController
@RequestMapping("group")
public class GroupController {

    @Autowired
    private GroupService groupService;

    @Autowired
    private ClazzClient clazzClient;

    @GetMapping
    public List<GroupVO> groupList(){

        List<Group> groupList = groupService.list();
        List<GroupVO> collect = groupList.stream().map(group -> {
            GroupVO groupVO = new GroupVO();
            Integer clazzid = Integer.valueOf(group.getClazzid());
            Clazz clazz = clazzClient.getById(clazzid);
            BeanUtils.copyProperties(group, groupVO);
            groupVO.setClazz(clazz);
            return groupVO;
        }).collect(Collectors.toList());
        return collect;
    }


    @PostMapping
    public Group  addGroup(Group group){

        group.setCreatedate(new Date());
        groupService.save(group);
        return  group;

    }

    @GetMapping("{id}")
    public Group getById(@PathVariable Integer id){
        return   groupService.getById(id);
    }

}
